package com.ruitu.mall_98_1.product.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.beanu.arad.base.my_base.MyBaseActivity;
import com.beanu.arad.utils.ToastUtils;
import com.beanu.arad.widget.progressactivity.ProgressLayout;
import com.ruitu.mall_98_1.R;
import com.ruitu.mall_98_1.dao.api.APIRetrofit;
import com.ruitu.mall_98_1.product.adapter.CouponListAdapter;
import com.ruitu.router_module.AppHolder;
import com.ruitu.router_module.CommonUtils;
import com.ruitu.router_module.bean.api.BaseResult;
import com.ruitu.router_module.bean.api.SubscriberImpl;
import com.ruitu.router_module.bean.mine.Coupon;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class CouponListPlatformActivity extends MyBaseActivity {
    private RecyclerView rcv_coupon;
    private TextView tv_empty;

    private CouponListAdapter adapter;
    private List<Coupon> couponList = new ArrayList<>();

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_coupon_list_platform;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rcv_coupon = findViewById(R.id.rcv_coupon);
        tv_empty = findViewById(R.id.tv_empty);
        setHeadTitle("领券中心");

        rcv_coupon.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CouponListAdapter(this);
        adapter.page = 1;
        rcv_coupon.setAdapter(adapter);

        showProgress();
        reqData();
    }

    private void reqData() {
        APIRetrofit.getDefault().reqPlatformCoupon()
                .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SubscriberImpl<BaseResult<List<Coupon>>>() {
                    @Override
                    public void onError(Throwable e) {
                        hideProgress();
                        e.printStackTrace();
                    }

                    @Override
                    public void onNext(BaseResult<List<Coupon>> listBaseResult) {
                        hideProgress();
                        if (listBaseResult.isSuccess()) {
                            couponList = listBaseResult.getData();
                            adapter.setDataList(couponList);
                            if (isDataEmpty(couponList)) {
                                tv_empty.setVisibility(View.VISIBLE);
                            } else {
                                tv_empty.setVisibility(View.GONE);
                            }
                        } else {
                            ToastUtils.showShort(listBaseResult.getMsg());
                        }
                    }
                });
    }

    //领取优惠券
    public void reqGetCoupon(final Coupon coupon) {
        if (CommonUtils.isLoginToActivity(this)) {
            hideProgress();
            showProgressWithText(true, "领取中...");
            String accountId = AppHolder.instance().getUser().getAccountId();
            String couponId = coupon.getId();
            APIRetrofit.getDefault().reqGetCoupon(accountId, couponId)
                    .subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new SubscriberImpl<BaseResult>() {
                        @Override
                        public void onError(Throwable e) {
                            hideProgress();
                        }

                        @Override
                        public void onNext(BaseResult baseResult) {
                            hideProgress();
                            if (baseResult.isSuccess()) {
                                ToastUtils.showShort("领券成功!");
                            } else {
                                ToastUtils.showShort(baseResult.getMsg());
                            }
                        }
                    });
        } else {
            ToastUtils.showShort("请先登录");
        }
    }

    protected ProgressLayout findProgressLayout() {
        return null;
    }
}

